<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\PatientInfo;
use App\PatientCase;
use Redirect,Auth,Input;
use DB;


class PatientController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        //
        $patients = PatientInfo::paginate(10);
        return view('user.patient.list')->with('patients',$patients);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //
        return view('user.patient.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store(Request $request)
    {
        //
        $this->validate($request,[
            'firstname'=>'required',
            'lastname' =>'required',
            ]);

        $patient = new PatientInfo;
     
        $patient->first_name = Input::get('firstname');
        $patient->last_name = Input::get('lastname');
        $patient->check_num =Input::get('checkNumber');
        $patient->sex = Input::get('sex');
        $patient->age =Input::get('age');
        $patient->reg_time = Input::get('regDate');
        $patient->last_check_time = Input::get('lastCheckDate');
        $patient->eyesight_left = Input::get('eyeSightLeft');
        $patient->eyesight_right = Input::get('eyeSightRight');
        $patient->contact_num = Input::get('contact');
        $patient->address = Input::get('address');
        $patient->description = trim(Input::get('description'));
        if($patient->save()){
            return Redirect::to('/');
        } else {
            return Redirect::back()->withErrors('Save Failed!');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
        $patient = PatientInfo::find($id);
        $cases = DB::table('patient_case')->where('patient_id','=',$id)->orderBy('updated_at','desc')->paginate(3);
        return view('user.patient.info')->with('patient',$patient)->with('cases',$cases);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
		$patient = PatientInfo::find($id);
		return view('user.patient.edit')->with('patient',$patient);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request,$id)
    {
        //
        $this->validate($request,[
            'firstname'=>'required',
            'lastname' =>'required',
            ]);

        $patient = PatientInfo::find($id);

        if(is_null($patient)){
            return Redirect::back()->withErrors('No matched patient info.');
        } else {
            $patient->first_name = Input::get('firstname');
            $patient->last_name = Input::get('lastname');
            $patient->check_num =Input::get('checkNumber');
            $patient->sex = Input::get('sex');
            $patient->age =Input::get('age');
            $patient->reg_time = Input::get('regDate');
            $patient->last_check_time = Input::get('lastCheckDate');
            $patient->eyesight_left = Input::get('eyeSightLeft');
            $patient->eyesight_right = Input::get('eyeSightRight');
            $patient->contact_num = Input::get('contact');
            $patient->address = Input::get('address');
			$patient->description = trim(Input::get('description'));
        }

        if($patient->save()){
            return Redirect::To('/patient/info/'.$id);
        } else {
            return Redirect::back()->withErrors('Save Failed!');
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //
        $patient = PatientInfo::find($id);

        $patient->delete();

        return Redirect::to('/');
    }
        
}
